package net.timeslicer.jabber;

import java.io.FileInputStream;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smackx.muc.MultiUserChat;

public class BombThread implements Runnable {

	static Thread t;
	static StringBuffer messageBuffer = new StringBuffer();
	static ConnectionConfiguration config;
	static XMPPConnection connection;
	static MultiUserChat room;
	private static Logger log = Logger.getLogger(BombThread.class);

	String host;
	//host=meebo.org
	
	int port;
	//port=5222
	
	String serviceName;
	//serviceName=meebo.org

	String user;
	//user=chris.topher
	
	String password;
	//password=#######

	String roomUrl;
	//room=intelligentadults@conference.gc11.meebo.org
	
	String botName;
	//botName=IA.meeBOT
	
	String propsFilePath;	
	
	public static void main(String[] args) {
		t = new Thread(new BombThread(args[0]));
		t.start();
	}

	private BombThread(String path) {
		this.propsFilePath = path;
	}
	@Override
	public void run() {
		try {
			Properties properties = new Properties();

			try {
				properties.load(new FileInputStream(this.propsFilePath));
				this.botName = properties.getProperty("botName");
				this.host = properties.getProperty("host");
				this.port = Integer.parseInt(properties.getProperty("port"));
				this.password = properties.getProperty("password");
				this.user = properties.getProperty("user");
				this.serviceName = properties.getProperty("serviceName");
				this.roomUrl = properties.getProperty("roomUrl");

			} catch (Exception e) {
				e.printStackTrace();
			}

			try {
				config = new ConnectionConfiguration(this.host, this.port,
						this.serviceName);

			} catch (Exception iggy) {
				iggy.printStackTrace();
			}

			int i = 11;
			while (true) {
				try {
					connection = new XMPPConnection(config);
					connection.connect();
					connection.getSASLAuthentication()
							.authenticateAnonymously();
					// t.sleep(500);
					room = new MultiUserChat(connection,
							this.roomUrl);
					
					String sTime = new Long(System.currentTimeMillis())
							.toString();
					sTime = StringUtils.reverse(sTime);
					String name = "guest" + sTime;
					name = name.substring(0, 12);
					try {
						room.join(name);
					} catch (Exception e) {
					}
					room
							.sendMessage("We're tired of seekcompanion spamming their links.\r\r\r\r\r\r\r\r\r\r\r\r" +
									"\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r \u202E");

					t.sleep(1200);
					room.leave();
					room = null;
					connection = null;
				} catch (Exception iggy) {
					log.error(iggy);

				}
			}
		} catch (Exception e) {
			e.printStackTrace();

		}
	}

}
